using System;
using Yoozoo.UI;
public class UIBinder
{

    private static ObjectPool<GameObjectBinder>  _gameObjectBinderPool = new ObjectPool<GameObjectBinder>(32);
    private static ObjectPool<UIExpansionBinder> _UIExpansionBinderPool = new ObjectPool<UIExpansionBinder>(16);
    private static ObjectPool<ListViewBinder> _listViewBinderPool = new ObjectPool<ListViewBinder>(2);
    private static ObjectPool<YGridViewBinder> _YGridViewBinderPool = new ObjectPool<YGridViewBinder>(2);
    private static ObjectPool<YImageBinder> _YImageBinderPool = new ObjectPool<YImageBinder>(32);
    private static ObjectPool<YListBinder> _YListBinderPool = new ObjectPool<YListBinder>(2);
    private static ObjectPool<YModuleContainerBinder> _YModuleContainerBinderPool = new ObjectPool<YModuleContainerBinder>(2);
    private static ObjectPool<YRawImageBinder> _YRawImageBinderPool = new ObjectPool<YRawImageBinder>(4);
    private static ObjectPool<YTableViewBinder> _YTableViewBinderPool = new ObjectPool<YTableViewBinder>(4);
    private static ObjectPool<ImageBinder> _ImageBinderPool = new ObjectPool<ImageBinder>(64);
    private static ObjectPool<RawImageBinder> _RawImageBinderPool = new ObjectPool<RawImageBinder>(16);
    private static ObjectPool<ButtonBinder> _ButtonBinderPool = new ObjectPool<ButtonBinder>(32);
    private static ObjectPool<CanvasBinder> _CanvasBinderPool = new ObjectPool<CanvasBinder>(4);
    private static ObjectPool<DOTweenAnimationBinder> _DOTweenAnimationBinderPool = new ObjectPool<DOTweenAnimationBinder>(4);
    private static ObjectPool<DropdownBinder> _DropdownBinderPool = new ObjectPool<DropdownBinder>(4);
    private static ObjectPool<InputFieldBinder> _InputFieldBinderPool = new ObjectPool<InputFieldBinder>(4);
    private static ObjectPool<RectTransformBinder> _RectTransformBinderPool = new ObjectPool<RectTransformBinder>(32);
    private static ObjectPool<ScrollbarBinder> _ScrollbarBinderPool = new ObjectPool<ScrollbarBinder>(2);
    private static ObjectPool<SpriteRendererBinder> _SpriteRendererBinderPool = new ObjectPool<SpriteRendererBinder>(0);
    private static ObjectPool<TextMeshProUGUIBinder> _TextMeshProUGUIBinderPool = new ObjectPool<TextMeshProUGUIBinder>(8);
    private static ObjectPool<YAnimationBinder> _YAnimationBinderPool = new ObjectPool<YAnimationBinder>(4);
    private static ObjectPool<YButtonBinder> _YButtonBinderPool = new ObjectPool<YButtonBinder>(16);
    private static ObjectPool<YCaptureContainerBinder> _YCaptureContainerBinderPool = new ObjectPool<YCaptureContainerBinder>(32);
    private static ObjectPool<YDragButtonBinder> _YDragButtonBinderPool = new ObjectPool<YDragButtonBinder>(2);
    private static ObjectPool<YEffectContainerBinder> _YEffectContainerBinderPool = new ObjectPool<YEffectContainerBinder>(0);
    private static ObjectPool<YPrefabContainerBinder> _YPrefabContainerBinderPool = new ObjectPool<YPrefabContainerBinder>(0);
    private static ObjectPool<YRatingBinder> _YRatingBinderPool = new ObjectPool<YRatingBinder>(0);
    private static ObjectPool<YRawRatingBinder> _YRawRatingBinderPool = new ObjectPool<YRawRatingBinder>(0);
    private static ObjectPool<YSliderBinder> _YSliderBinderPool = new ObjectPool<YSliderBinder>(0);
    private static ObjectPool<YTextBinder> _YTextBinderPool = new ObjectPool<YTextBinder>(64);
    private static ObjectPool<YToggleBinder> _YToggleBinderPool = new ObjectPool<YToggleBinder>(4);
    private static ObjectPool<YToggleGroupBinder> _YToggleGroupBinderPool = new ObjectPool<YToggleGroupBinder>(2);

    public static void Register()
    {
        UIExpansionUtility.getBinderFromPoolDelegate = GetBinderFromPoolByBinderName;
        UIExpansionUtility.recoverBinderDelegate = RecoverBinderDelegate;
        
        UIExpansionUtility.RegisterBinder(typeof(GameObjectBinder));
        UIExpansionUtility.RegisterBinder(typeof(UIExpansionBinder));
        UIExpansionUtility.RegisterBinder(typeof(ListViewBinder));
        UIExpansionUtility.RegisterBinder(typeof(YGridViewBinder));
        UIExpansionUtility.RegisterBinder(typeof(YImageBinder));
        UIExpansionUtility.RegisterBinder(typeof(YListBinder));
        UIExpansionUtility.RegisterBinder(typeof(YModuleContainerBinder));
        UIExpansionUtility.RegisterBinder(typeof(YRawImageBinder));
        UIExpansionUtility.RegisterBinder(typeof(YTableViewBinder));
        UIExpansionUtility.RegisterBinder(typeof(ImageBinder));
        UIExpansionUtility.RegisterBinder(typeof(RawImageBinder));
        UIExpansionUtility.RegisterBinder(typeof(ButtonBinder));
        UIExpansionUtility.RegisterBinder(typeof(CanvasBinder));
        UIExpansionUtility.RegisterBinder(typeof(DOTweenAnimationBinder));
        UIExpansionUtility.RegisterBinder(typeof(DropdownBinder));
        UIExpansionUtility.RegisterBinder(typeof(InputFieldBinder));
        UIExpansionUtility.RegisterBinder(typeof(RectTransformBinder));
        UIExpansionUtility.RegisterBinder(typeof(ScrollbarBinder));
        UIExpansionUtility.RegisterBinder(typeof(SpriteRendererBinder));
        UIExpansionUtility.RegisterBinder(typeof(TextMeshProUGUIBinder));
        UIExpansionUtility.RegisterBinder(typeof(YAnimationBinder));
        UIExpansionUtility.RegisterBinder(typeof(YButtonBinder));
        UIExpansionUtility.RegisterBinder(typeof(YCaptureContainerBinder));
        UIExpansionUtility.RegisterBinder(typeof(YDragButtonBinder));
        UIExpansionUtility.RegisterBinder(typeof(YEffectContainerBinder));
        UIExpansionUtility.RegisterBinder(typeof(YPrefabContainerBinder));
        UIExpansionUtility.RegisterBinder(typeof(YRatingBinder));
        UIExpansionUtility.RegisterBinder(typeof(YRawRatingBinder));
        UIExpansionUtility.RegisterBinder(typeof(YSliderBinder));
        UIExpansionUtility.RegisterBinder(typeof(YTextBinder));
        UIExpansionUtility.RegisterBinder(typeof(YToggleBinder));
        UIExpansionUtility.RegisterBinder(typeof(YToggleGroupBinder));
    }

    private static void RecoverBinderDelegate(Type type, BinderBase binderbase)
    {
        if (type == typeof(GameObjectBinder))
        {
            _gameObjectBinderPool.Release(binderbase as GameObjectBinder);
        }
        else if (type == typeof(UIExpansionBinder))
        {
           _UIExpansionBinderPool.Release(binderbase as UIExpansionBinder);
        }
        else if (type == typeof(ListViewBinder))
        {
            _listViewBinderPool.Release(binderbase as ListViewBinder);
        }
        else if (type == typeof(YGridViewBinder))
        {
            _YGridViewBinderPool.Release(binderbase as YGridViewBinder);
        }else if (type == typeof(YImageBinder))
        {
            _YImageBinderPool.Release(binderbase as YImageBinder);
        }else if (type == typeof(YListBinder))
        {
            _YListBinderPool.Release(binderbase as YListBinder);
        }else if (type == typeof(YModuleContainerBinder))
        {
            _YModuleContainerBinderPool.Release(binderbase as YModuleContainerBinder);
        }else if (type == typeof(YRawImageBinder))
        {
            _YRawImageBinderPool.Release(binderbase as YRawImageBinder);
        }else if (type == typeof(YTableViewBinder))
        {
            _YTableViewBinderPool.Release(binderbase as YTableViewBinder);
        }else if (type == typeof(ImageBinder))
        {
            _ImageBinderPool.Release(binderbase as ImageBinder);
        }else if (type == typeof(RawImageBinder))
        {
            _RawImageBinderPool.Release(binderbase as RawImageBinder);
        }else if (type == typeof(ButtonBinder))
        {
            _ButtonBinderPool.Release(binderbase as ButtonBinder);
        }else if (type == typeof(CanvasBinder))
        {
            _CanvasBinderPool.Release(binderbase as CanvasBinder);
        }else if (type == typeof(DOTweenAnimationBinder))
        {
            _DOTweenAnimationBinderPool.Release(binderbase as DOTweenAnimationBinder);
        }else if (type == typeof(DropdownBinder))
        {
            _DropdownBinderPool.Release(binderbase as DropdownBinder);
        }else if (type == typeof(InputFieldBinder))
        {
            _InputFieldBinderPool.Release(binderbase as InputFieldBinder);
        }else if (type == typeof(RectTransformBinder))
        {
            _RectTransformBinderPool.Release(binderbase as RectTransformBinder);
        }else if (type == typeof(ScrollbarBinder))
        {
            _ScrollbarBinderPool.Release(binderbase as ScrollbarBinder);
        }else if (type == typeof(SpriteRendererBinder))
        {
            _SpriteRendererBinderPool.Release(binderbase as SpriteRendererBinder);
        }else if (type == typeof(TextMeshProUGUIBinder))
        {
            _TextMeshProUGUIBinderPool.Release(binderbase as TextMeshProUGUIBinder);
        }else if (type == typeof(YAnimationBinder))
        {
             _YAnimationBinderPool.Release(binderbase as YAnimationBinder);
        }else if (type == typeof(YButtonBinder))
        {
            _YButtonBinderPool.Release(binderbase as YButtonBinder);
        }else if (type == typeof(YCaptureContainerBinder))
        {
            _YCaptureContainerBinderPool.Release(binderbase as YCaptureContainerBinder);
        }else if (type == typeof(YDragButtonBinder))
        {
            _YDragButtonBinderPool.Release(binderbase as YDragButtonBinder);
        }else if (type == typeof(YEffectContainerBinder))
        {
            _YEffectContainerBinderPool.Release(binderbase as YEffectContainerBinder);
        }else if (type == typeof(YPrefabContainerBinder))
        {
            _YPrefabContainerBinderPool.Release(binderbase as YPrefabContainerBinder);
        }else if (type == typeof(YRatingBinder))
        {
            _YRatingBinderPool.Release(binderbase as YRatingBinder);
        }else if (type == typeof(YRawRatingBinder))
        {
            _YRawRatingBinderPool.Release(binderbase as YRawRatingBinder);
        }else if (type == typeof(YRatingBinder))
        {
            _YRatingBinderPool.Release(binderbase as YRatingBinder);
        }else if (type == typeof(YSliderBinder))
        {
            _YSliderBinderPool.Release(binderbase as YSliderBinder);
        }else if (type == typeof(YTextBinder))
        { 
            _YTextBinderPool.Release(binderbase as YTextBinder);
        }else if (type == typeof(YToggleBinder))
        {
            _YToggleBinderPool.Release(binderbase as YToggleBinder);
        }else if (type == typeof(YToggleGroupBinder))
        {
            _YToggleGroupBinderPool.Release(binderbase as YToggleGroupBinder);
        }
    }

    private static BinderBase GetBinderFromPoolByBinderName(Type type)
    {
        if (type == typeof(GameObjectBinder))
        {
            return _gameObjectBinderPool.Get();
        }
        else if (type == typeof(UIExpansionBinder))
        {
            return _UIExpansionBinderPool.Get();
        }else if (type == typeof(ListViewBinder))
        {
            return _listViewBinderPool.Get();
        }else if (type == typeof(YGridViewBinder))
        {
            return _YGridViewBinderPool.Get();
        }else if (type == typeof(YImageBinder))
        {
            return _YImageBinderPool.Get();
        }else if (type == typeof(YListBinder))
        {
            return _YListBinderPool.Get();
        }else if (type == typeof(YModuleContainerBinder))
        {
            return _YModuleContainerBinderPool.Get();
        }else if (type == typeof(YRawImageBinder))
        {
            return _YRawImageBinderPool.Get();
        }else if (type == typeof(YTableViewBinder))
        {
            return _YTableViewBinderPool.Get();
        }else if (type == typeof(ImageBinder))
        {
            return _ImageBinderPool.Get();
        }else if (type == typeof(RawImageBinder))
        {
            return _RawImageBinderPool.Get();
        }else if (type == typeof(ButtonBinder))
        {
            return _ButtonBinderPool.Get();
        }else if (type == typeof(CanvasBinder))
        {
            return _CanvasBinderPool.Get();
        }else if (type == typeof(DOTweenAnimationBinder))
        {
            return _DOTweenAnimationBinderPool.Get();
        }else if (type == typeof(DropdownBinder))
        {
            return _DropdownBinderPool.Get();
        }else if (type == typeof(InputFieldBinder))
        {
            return _InputFieldBinderPool.Get();
        }else if (type == typeof(RectTransformBinder))
        {
            return _RectTransformBinderPool.Get();
        }else if (type == typeof(ScrollbarBinder))
        {
            return _ScrollbarBinderPool.Get();
        }else if (type == typeof(SpriteRendererBinder))
        {
            return _SpriteRendererBinderPool.Get();
        }else if (type == typeof(TextMeshProUGUIBinder))
        {
            return _TextMeshProUGUIBinderPool.Get();
        }else if (type == typeof(YAnimationBinder))
        {
            return _YAnimationBinderPool.Get();
        }else if (type == typeof(YButtonBinder))
        {
            return _YButtonBinderPool.Get();
        }else if (type == typeof(YCaptureContainerBinder))
        {
            return _YCaptureContainerBinderPool.Get();
        }else if (type == typeof(YDragButtonBinder))
        {
            return _YDragButtonBinderPool.Get();
        }else if (type == typeof(YEffectContainerBinder))
        {
            return _YEffectContainerBinderPool.Get();
        }else if (type == typeof(YPrefabContainerBinder))
        {
            return _YPrefabContainerBinderPool.Get();
        }else if (type == typeof(YRatingBinder))
        {
            return _YRatingBinderPool.Get();
        }else if (type == typeof(YRawRatingBinder))
        {
            return _YRawRatingBinderPool.Get();
        }else if (type == typeof(YRatingBinder))
        {
            return _YRatingBinderPool.Get();
        }else if (type == typeof(YSliderBinder))
        {
            return _YSliderBinderPool.Get();
        }else if (type == typeof(YTextBinder))
        {
            return _YTextBinderPool.Get();
        }else if (type == typeof(YToggleBinder))
        {
            return _YToggleBinderPool.Get();
        }else if (type == typeof(YToggleGroupBinder))
        {
            return _YToggleGroupBinderPool.Get();
        }else
        {
            return null;
        }
    }
    
    #if UNITY_EDITOR
    [UnityEditor.Callbacks.DidReloadScripts]
    static void AllScriptsReloaded()
    {
        Register();
    }
    #endif
}
